
ConfigPageVars.valueTypes.add("number", "数字", function(valueType) {

	var v = { metadataOptions:{}, configValueInit:null };

	// 配置值的初始化处理
	v.configValueInit = function(valueMetadata) {
		if (!valueMetadata) { return; }
		var $configValue = $(this);
		// 小数位数
		var newDecimal = valueMetadata ? valueMetadata.decimal || 0 : 0;
		// 设置metaRule用于校验
		$configValue.xcall({ disabled:false, precision:newDecimal, metaRule:valueMetadata });
	};

	// 配置项元数据编辑器
	v.metadataOptions.onValueChange = function(newValue, oldValue) {
		var form = $(this).closest("form");
		var configValue = form.find(".show-in-" + valueType).find("input[textboxname=configValue]");
		if (configValue.length == 0) { return; }
		v.configValueInit.call(configValue, newValue);
	};
	v.metadataOptions.onAfterRenderPanel = function(panel) {
    	var panelOpts = $(this).xcall("options");
    	var space = panelOpts.xref.space;
		space.xui(panel);
	};
	v.metadataOptions.onBindEvent = function(panel) {
		var combo = $(this);
		// 保存按钮
		panel.on("click", ".combo-form a.do-save", function() {
			var form = panel.find(".combo-form form");
			form.form("enableValidation");
			if (!form.form("validate")) { return; }
			var json = form.serializeJson();
			var buffer = {};
			if (json.decimal) { buffer.decimal = json.decimal * 1; }
			if (json.minValue) { buffer.minValue = json.minValue * 1; }
			if (json.maxValue) { buffer.maxValue = json.maxValue * 1; }
			if ($.isEmptyObject(buffer)) { buffer = null; }
			combo.xcall("setValue", buffer);
    		combo.xcall("hidePanel");
		});
		// 取消按钮
		panel.on("click", ".combo-form a.do-cancel", function() {
			combo.xcall("hidePanel");
		});
	};
    // value转换为文本, 用于setText调用
	v.metadataOptions.formatter = function(json) {
		if (!json || $.isEmptyObject(json)) { return ""; }
		var buffer = [];
		if (json.decimal != undefined) { buffer.push("小数位数: " + json.decimal); }
		if (json.minValue != undefined) { buffer.push("最小值: " + json.minValue); }
		if (json.maxValue != undefined) { buffer.push("最大值: " + json.maxValue); }
        var opts = $(this).comboform("options");
        return buffer.join(opts.separator);
	};
	return v;
});


$.extend($.fn.validatebox.defaults.rules, {
    numberValueValidate: {
        validator: function(value, param) {
            var opts = $(this).validatebox("options");
        	var me = $(this);
        	var value = me.val();
        	if (!value) { return true; }
        	var metaRule = opts.metaRule;
        	if (!metaRule || $.isEmptyObject(metaRule)) { return true; }

        	var number = value * 1;
        	var minValue = metaRule.minValue;
        	var maxValue = metaRule.maxValue;
        	if (minValue != undefined && maxValue != undefined) {
        		if (number < minValue || number > maxValue) {
        			opts.invalidMessage = $.zhh.format("数值必须介于{0}和{1}之间", minValue, maxValue);
        			return false;
        		}
        	} else if (minValue != undefined) {
        		if (number < minValue) {
        			opts.invalidMessage = "数值不能小于" + minValue;
        			return false;
        		}
        	} else if (maxValue != undefined) {
        		if (number > maxValue) {
        			opts.invalidMessage = "数值不能大于" + maxValue;
        			return false;
        		}
        	}
            return true;
        }
    }
});
